package com.pospi.xstore.ui.static_ui;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

import com.pospi.xstore.R;
import com.pospi.xstore.adapter.SaleRankingAdapter;
import com.pospi.xstore.dao.OrderDao;
import com.pospi.xstore.dto.GoodsDto;
import com.pospi.xstore.dto.OrderDto;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.util.GetData;
import com.pospi.xstore.util.SaveOrderInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SaleRankingActivity extends BaseActivity {

    @Bind(R.id.lv_goods)
    ListView lvGoods;
    @Bind(R.id.sale_ranking_data)
    TextView saleRankingData;
    @Bind(R.id.sale_ranking_data_to)
    TextView DataTo;
    @Bind(R.id.search)
    Button search;

    public final static int FROM = 121;
    public final static int TO = 122;


    private SaleRankingAdapter adapter;

    private int fromData;
    private int toData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_sale_ranking);
        setTitle("产品销售排行");
        ButterKnife.bind(this);

        saleRankingData.setText(GetData.getYYMMDDTime());
        fromData = GetData.getSearchTime();

        DataTo.setText(GetData.getYYMMDDTime());
        toData = GetData.getSearchTime();
        addAdapter();

    }


    public void showPickerDialog(final TextView tv, final int num) {
        final Calendar c1 = Calendar.getInstance();
        DatePickerDialog dialog1 = new DatePickerDialog(SaleRankingActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                c1.set(year, monthOfYear, dayOfMonth);
                tv.setText(DateFormat.format("yyyy-MM-dd", c1));
                switch (num) {
                    case FROM:
                        fromData = Integer.parseInt(DateFormat.format("yyyyMMdd", c1).toString());
                        break;
                    case TO:
                        toData = Integer.parseInt(DateFormat.format("yyyyMMdd", c1).toString());
                        break;
                }
            }
        }, c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH));
        dialog1.show();

    }


    public void addAdapter() {
        Log.d("todaysale", "开始绑定数据");
        //开始查询数据，从数据库中查询订单的信息
        List<OrderDto> dtos = new OrderDao(getApplicationContext()).selectGoodsBetween(fromData, toData);
        Log.d("数据大小", "size:" + dtos.size());
        List<GoodsDto> goodsDtoList = new ArrayList<>();
        for (int i = 0; i < dtos.size(); i++) {
            String orderInfo = dtos.get(i).getOrder_info();
            List<GoodsDto> goodsDtos = SaveOrderInfo.changeToList(orderInfo);
            goodsDtoList.addAll(goodsDtos);
        }
        //把每次提交的订单累加在一起
        Log.i("size", goodsDtoList.size() + "");
        for (int i = 0; i < goodsDtoList.size(); i++) {
            GoodsDto good1 = goodsDtoList.get(i);
            String good_name1 = good1.getName();
            for (int j = goodsDtoList.size() - 1; j > i; j--) {
                GoodsDto good2 = goodsDtoList.get(j);
                String good_name2 = good2.getName();
                if (good_name1.equals(good_name2)) {
                    good1.setNum(good1.getNum() + good2.getNum());
                    goodsDtoList.remove(good2);
                }
            }
        }

        Collections.sort(goodsDtoList, new Comparator<GoodsDto>() {
            @Override
            public int compare(GoodsDto user1, GoodsDto user2) {
                Double id1 = user1.getNum();
                Double id2 = user2.getNum();
                //可以按User对象的其他属性排序，只要属性支持compareTo方法
                return id2.compareTo(id1);
            }
        });
        adapter = new SaleRankingAdapter(getApplicationContext(), goodsDtoList);
        lvGoods.setAdapter(adapter);

    }

    @OnClick({R.id.sale_ranking_data, R.id.sale_ranking_data_to, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sale_ranking_data:
                showPickerDialog(saleRankingData, FROM);
                break;
            case R.id.sale_ranking_data_to:
                showPickerDialog(DataTo, TO);
                break;
            case R.id.search:
                if (toData >= fromData & toData <= GetData.getSearchTime()) {
                    addAdapter();
                } else {
                    showToast("请选择正确的查询日期");
                }
                break;
        }
    }
}
